package MyRemoteSorter;
import java.rmi.RemoteException;
import java.rmi.server.*;

public class InsertionSort extends UnicastRemoteObject implements ISorter
{
        public InsertionSort() throws RemoteException {
                super();
        }

        @Override
        public Comparable[] sort(Comparable[] comparable)
        {
        	if (comparable == null)
    			throw new IllegalArgumentException("De array is leeg.");
    		else {
                Comparable[] returnedComparable = comparable;
                for (int arrayIndex = 1; arrayIndex < returnedComparable.length; arrayIndex++)
                {
                    int arrayIndexCompy = arrayIndex;
                    Comparable tempComparable = returnedComparable[arrayIndex];
                    while ((arrayIndexCompy > 0) && (returnedComparable[arrayIndexCompy-1].compareTo(tempComparable) > 0))
                    {
                        returnedComparable[arrayIndexCompy] = returnedComparable[arrayIndexCompy-1];
                        arrayIndexCompy--;
                    }
                    returnedComparable[arrayIndexCompy] = tempComparable;
            }
                return returnedComparable;
        }
        }
}
